﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConverterInterface;

namespace CSharpConverterLibrary
{
    public class IdentifierState : State
    {
        private IdentifierState()  { }

        private static IdentifierState instance = new IdentifierState();

        private char c;

        public static IdentifierState Instance
        {
            get {return instance;}
        }

        public override void Execute(CSharpConverter owner)
        {
            if (owner.AvailableChars() <= 0)
            {
                owner.machine.Finished = true;
                owner.machine.ChangeState(NothingState.Instance);
            }

            else
            {
                c = owner.Read();

                if (char.IsLetter(c) || char.IsDigit(c) || c == '_')  // -> continue the identifier
                {
                    owner.machine.Buffer.Append(c);
                }

                else if (c == '\"') // -> string
                {
                    owner.machine.ChangeState(StringState.Instance);
                }

                else if (c == '\'') // -> char
                {
                    owner.machine.ChangeState(CharState.Instance);
                }

                else if (c == '/') // -> commentary ?
                {
                    if (owner.AvailableChars() > 0)
                    {
                        if (owner.Peek() == '/')
                        {
                            owner.machine.ChangeState(SinglelineCommentState.Instance);
                            owner.machine.Buffer.Append(c);
                            owner.Forward();
                        }
                        else if (owner.Peek() == '*')
                        {
                            owner.machine.ChangeState(MultilineCommentState.Instance);
                            owner.machine.Buffer.Append(owner.Read());
                            
                        }
                    }
                }

                else // -> nothing
                {
                    owner.machine.ChangeState(NothingState.Instance);
                }

              
            }
        }

        public override void Exit(CSharpConverter owner)
        {
            if (owner.KeywordInBuffer())
            {
                owner.machine.Buffer.Insert(0, "<span class=\'keyword\'>");
                owner.machine.Buffer.Append("</span>");
            }
                      
            owner.FlushBuffer();
            owner.machine.Buffer.Append(c);
        }

    }
}
